<?php

class Model_Course extends Model_Dec {

	const TABLE = "course";
	
	/**
	 * @var protected $id id value
	 * @method int get_id() get_id(void)
	 * @method void set_id() set_id(int $id)
	 */
	protected $id;
	
	/**
	 * @var protected $institution_id institution_id value
	 * @method int get_institution_id() get_institution_id(void)
	 * @method void set_institution_id() set_institution_id(int $institution_id)
	 */
	protected $institution_id;
	
	/**
	 * @var protected $title title value
	 * @method string get_title() get_title(void)
	 * @method void set_title() set_title(string $title)
	 */
	protected $title;
	
	/**
	 * @var protected $code code value
	 * @method string get_code() get_code(void)
	 * @method void set_code() set_code(string $code)
	 */
	protected $code;
	
	/**
	 * @var protected $created_in created_in value
	 * @method datetime get_created_in() get_created_in(void)
	 * @method void set_created_in() set_created_in(datetime $created_in)
	 */
	protected $created_in;
	
	/**
	 * @var protected $user_id user_id value
	 * @method int get_user_id() get_user_id(void)
	 * @method void set_user_id() set_user_id(int $user_id)
	 */
	protected $user_id;
	
	/**
	 * @var protected $credits credits value
	 * @method int get_credits() get_credits(void)
	 * @method void set_credits() set_credits(int $credits)
	 */
	protected $credits;

	/**
	 * @return Model_Course
	 */
	public function __construct($id=0) {
		if(0<intval($id)) {
			$db = DB::select("*")->from(self::TABLE)->where("id", "=", $id)->limit(1);
			$db = $db->as_assoc()->execute();
			if($db->count()>0) {
				$this->load($db->current());
			}
		}
	}

	protected static function get_database_fields() {
		return array(
			'institution_id' => __("Course_Institution_id"),
			'title' => __("Course_Title"),
			'code' => __("Course_Code"),
			'created_in' => __("Course_Created_in"),
			'user_id' => __("Course_User_id"),
			'credits' => __("Course_Credits"),
		);
	}

	public function validate() {
		$validateArray = array();
		foreach (self::get_database_fields() as $key => $value) {
			$validateArray[$key] = $this->get($key);
		}
		$validation = Validation::factory($validateArray)
				->rule('created_in', 'not_empty')
				->rule('user_id', 'not_empty')
				->rule('credits', 'not_empty')
				;
		if ($validation->check()) {
			return $validateArray;
		} else {
			throw new Validation_Exception('validation', $validation);
		}
	}

	public static function get_report() {
		return DB::select(
				"id"
				, "institution_id"
				, "title"
				, "code"
				, "created_in"
				, "user_id"
				, "credits"
				, array("id", "actions"))
		->from(self::TABLE)
		->order_by("id", "ASC");
	}
        
        public static function get_institution_courses($institution_id,$only_ids=false){
            $courses_sql = DB::select($only_ids?"id":"*")
                ->from(self::TABLE)
                ->where("institution_id","=",$institution_id)->order_by("id","DESC")->execute();
            
            $courses = array();
            foreach($courses_sql as $i=>$course){
                $courses[$i] = $course;
                $courses[$i]['classes'] = Model_Class::get_course_classes($course['id'],$only_ids);
            }
            return $courses;
        }
        public static function load_course_as_assoc($course_id, $user_id){
            $course = DB::select("*")->from("course")
                    ->where("course.id","=",$course_id)
                    ->and_where("course.user_id","=",$user_id)
                    ->as_assoc()->execute()->current();
            $course['classes'] = DB::select("id")->from("class")
                    ->where("course_id","=",$course_id)->execute()->as_array();
            foreach($course['classes'] as $i=>$class){
                $course['classes'][$i] = Model_Class::load_class_as_assoc($class['id'], $user_id, true);
            }
            return $course;
        }
        public function get_classes(){
            return Model_Class::get_course_classes($this->id);
        }
        public function as_array(){
            $array = array();
            $array["id"] = $this->id;
            $array["code"] = $this->code;
            $array["created_id"] = $this->created_in;
            $array["credits"] = $this->credits;
            $array["institution_id"] = $this->institution_id;
            $array["title"] = $this->title;
            $array["user_id"] = $this->user_id;
            $array["classes"] = Model_Class::get_course_classes($this->id);
            return $array;
        }
        //OVERRIDE
        public function remove(){
            $classes = Model_Class::get_course_classes($this->id, true);
            if(sizeof($classes)>0){
                DB::delete("class_hours")->where("class_id","in",$classes)->execute();
                DB::delete("class")->where("id","in",$classes)->execute();
            }
            parent::remove();
        }
}